﻿// -----------------------------------------------------------------------
// <copyright file="ReportableCountTaskHelper.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------


using System.Diagnostics;
using System.Diagnostics.Contracts;
using Dhgms.TplHelper.Model.Info.TaskResult;

namespace Dhgms.TplHelper.Model.TaskHelper
{
    using System;
    using System.Threading.Tasks;
    using Dhgms.TplHelper.Model.Info;
    using Dhgms.TplHelper.Model.Info.JobResult;

    using Splat;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public abstract class ReportableCountTaskHelper<TTaskResultClass, TProcessResultClass> : BaseTaskHelper<TTaskResultClass>, IEnableLogger
        where TTaskResultClass : TaskResultBase, IReportableCount, new()
        where TProcessResultClass : IReportableCount
    {
        /// <summary>Initializes a new instance of the <see cref="ReportableCountTaskHelper{TTaskResultClass, TProcessResultClass}"/> class.</summary>
        /// <param name="name">The name.</param>
        protected ReportableCountTaskHelper(string name)
            : base(name)
        {
        }

        /// <summary>Entry point for doing the donkey work.</summary>
        /// <returns>The Execution Result.</returns>
        public override async Task<TTaskResultClass> GetTaskResult()
        {
            this.Log().Info("Starting Task: " + Name);
            var process = GetProcess();
            process.Start();

            var result = new TTaskResultClass
            {
                Name = Name,
                Started = DateTime.Now,
            };

            var processResult = await process;

            result.ReportableCount = processResult.ReportableCount;
            await DoWorkOnProcessResult(result, processResult);

            result.Finished = DateTime.Now;
            this.Log().Info("Finished Task: " + Name);
            return result;
        }

        protected abstract Task DoWorkOnProcessResult(TTaskResultClass result, TProcessResultClass processResult);

        /// <summary>
        /// The OnProcess event for doing the donkey work
        /// </summary>
        protected abstract Task<TProcessResultClass> GetProcess();

        /// <summary>
        /// Executes a job, adds the result to the TaskResultCollection and returns the internal data. Used to streamline some
        /// common functionality.
        /// </summary>
        /// <param name="taskResultCollection">
        /// The task result collection.
        /// </param>
        /// <returns>
        /// The reportable count
        /// </returns>
        public async Task<long> GetResult(TaskCollection taskResultCollection)
        {
            Contract.Requires<ArgumentNullException>(taskResultCollection != null, "taskResultCollection");

            var taskResult = await this.GetTaskResult();
            taskResultCollection.TaskResults.Add(taskResult);

            return taskResult.ReportableCount;
        }
    }
}
